$(document).ready(function () {
    initLink();
})

function initLink() {
    $.ajax({
        type: 'get',
        url: '/api/link',
        dataType:"json",
        data:{
            "pageSize":20,
            "pageNumber":0
        },
        success: function (result) {
            var data = result.data;
            $("#link_list").html("")
            $("#link_account").html(result.totalCount)
            var val = "";
            data.forEach(function(value,index){
                var type =""
                if (value.articleType=="1"){
                    type="技术分享"
                } else if (value.articleType=="2") {
                    type="生活点滴"
                }else {
                    type="更新公告"
                }
                val = val+ "<div class=\"col-sm-4 col-md-3\">"+
                "<div class=\"thumbnail\">"+
                "<img class=\"url_login\" src=\""+value.icon+"\" alt=\"\">"+
                "<div class=\"caption\">"+
                "<p> 网站名称：<span>"+value.title+"</span> </p>"+
                "<p> 网站链接：<a class=\"url\" href=\""+ value.url+"\">"+value.url+"</a></p>"+
                    "<p> 网站简介：<span>"+value.synopsis+"</span> </p>"+
                    "<p><a role=\"button\" class=\"btn_open\" style=\"border: 1px solid #eee;padding: 2px 4px;margin: 0px 4px;\">"+
                "<span class='glyphicon glyphicon-eye-open'></span> 已显示 </a> </p>"+
                "<p> <a class=\"btn_edit\" onclick='querylinkById("+value.id+")' style=\"border: 1px solid #F37B1D;padding: 2px 4px;margin: 0px 4px;\" role=\"button\">"+
                "<span class='glyphicon glyphicon-file'></span> 编辑 </a>"+
                "<a class=\"btn_delete\"  onclick='deleteLink("+value.id+")'  style=\"border: 1px solid #e7505a;padding: 2px 4px;margin: 0px 4px;\" role=\"button\">"+
                "<span class='glyphicon glyphicon-trash'></span> 删除 </a> </p>"+
                "</div> </div> </div>"

            });
            $("#link_list").append(val)
        },
        error:function () {
            layer.msg("系统错误！",{icon: 5});
        }
    });
}



function querylinkById(id) {
    $.ajax({
        url:"/api/querylinkById/"+id,
        type:"get",
        dataType:"json",
        success:function(result){
            $(".xg_id").val(result.data.id);
            $(".xg_name").val(result.data.title);
            $(".xg_con").val(result.data.synopsis);
            $(".xg_logo").val(result.data.icon);
            $(".xg_url").val(result.data.url);
        },
        error:function () {
            layer.msg("系统错误！",{icon: 5});
        }
    });
}

function deleteLink(id) {
    $.ajax({
        url:"/api/deleteLink/"+id,
        type:"get",
        dataType:"json",
        success:function(result){
            initLink();
        },
        error:function () {
            layer.msg("系统错误！",{icon: 5});
        }
    });
}



$(function(){

    //通过append添加的HTML事件无效,使用on方法
    $(document).on("click", ".btn_edit", function () {
        $(".link_div").css({
            "display":"block",
        });
    });

	/*$(".btn_edit").click(function(){
		$(".link_div").css({
			"display":"block",
		});
	})*/
	
	$(".btn_qx").click(function(){
		$(".link_div").css({
			"display":"none",
		});
	})

    $(".btn_xg").click(function(){

        $.ajax({
            type: 'POST',
            url: '/api/updatelink',
            dataType:"json",
            data: {
                'id':  $(".xg_id").val(),
                'title': $(".xg_name").val(),
                'synopsis': $(".xg_con").val(),
                'icon':$(".xg_logo").val(),
                'url':$(".xg_url").val()
            },
            success: function (result) {
                if (result.code==200){
                    layer.msg(result.message,{icon: 1});
                    window.setTimeout(function () {
                        window.location.href="/admin/link"
                    },1000)
                } else {
                    layer.msg(result.message,{icon: 5});
                }

            },
            error:function () {
                layer.msg("系统错误！",{icon: 5});
            }
        });
    })



})
